import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutterdemo/service/http_request.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomeContent(),
    );
  }
}

class HomeContent extends StatefulWidget {
  @override
  _HomeContentState createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {

  void requestNetwork() async {
    // 1.创建HttpClient对象
    final httpClient = HttpClient();

    // 2.构建请求的uri
    final uri = Uri.parse("http://rest.apizza.net/mock/3bb78bf66bbd102a95221370ba9ddc2f/test");

    // 3.构建请求
    final request = await httpClient.getUrl(uri);

    // 4.发送请求，必须
    final response = await request.close();
    if (response.statusCode == HttpStatus.ok) {
      print(await response.transform(utf8.decoder).join());
    } else {
      print(response.statusCode);
    }

  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    requestNetwork();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
    );
  }
}
